package vsm.vsm_server.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vsm.vsm_server.mapper.NoticeMapper;
import vsm.vsm_server.pojo.Comment.CommentSendDto;
import vsm.vsm_server.pojo.Notice.Notice;
import vsm.vsm_server.pojo.Notice.NoticeVo;
import vsm.vsm_server.pojo.User.UserInfo;
import vsm.vsm_server.service.NoticeService;
import vsm.vsm_server.service.UserService;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class NoticeServiceImpl implements NoticeService {
    private NoticeMapper noticeMapper;

    public NoticeServiceImpl(NoticeMapper noticeMapper){
        this.noticeMapper = noticeMapper;
    }

    @Autowired
    UserService userService;
    @Override
    public List<NoticeVo> getAllNotice(BigInteger userId) {
        List<NoticeVo> noticeVoList = new ArrayList<>();
        List<Notice> noticeList = noticeMapper.getAllNotice(userId);
        if(!noticeList.isEmpty()){
            List<BigInteger> userInfoList = new ArrayList<>();
            for(Notice notice:noticeList){
                userInfoList.add(notice.getSenderId());
            }
            Map<BigInteger, UserInfo> userInfos =userService.getUserInfoList(userInfoList);
            for(Notice notice:noticeList){
                NoticeVo noticeVo = new NoticeVo();
                BeanUtils.copyProperties(notice,noticeVo);
                noticeVo.setSenderName(userInfos.get(noticeVo.getSenderId()).getUserName());
                noticeVo.setAvatar(userInfos.get(noticeVo.getSenderId()).getAvatar());
                noticeVoList.add(noticeVo);
            }
        }
        return noticeVoList;
    }

    //发送消息
    @Override
    public void saveNotice(Notice notice) {
        if(!Objects.equals(notice.getSenderId(), notice.getUserId())){
            if(notice.getType()==3){
                noticeMapper.saveCommentNotice(notice);
            }else{
                noticeMapper.saveNotice(notice);
            }

        }

        //根据类型判断行为模式
  /*      switch (notice.getType()){
            case 1:{

                break;
            }
            case 2:{

                break;
            }
            case 3:{

                break;
            }
            default:{

            }
        }*/

    }

    @Override
    public Boolean getExistNew(BigInteger userId) {
        return noticeMapper.getExistNew(userId);
    }

    @Override
    public void checkNewNotice(BigInteger userId) {
        noticeMapper.checkNewNotice(userId);
    }

    @Override
    public void saveCommentNotice(CommentSendDto commentSendDto) {
        //当前时间
        String datestr = commentSendDto.getCreateTime();
        //保存记录
        Notice notice = new Notice();
        notice.setTime(datestr);


        notice.setType(3);
        notice.setUserId(commentSendDto.getGetterId());
        notice.setSenderId(commentSendDto.getUserId());
        if(notice.getSenderId()!=notice.getUserId()){
            String content ="";
            if(commentSendDto.getRootCommentId()!=null) {
                if (!commentSendDto.getRootCommentId().equals(commentSendDto.getToCommentId())) {
                    content="回复了你：";
                }else{
                    content="回复了你：";
                }
            }else{
                content = "评论了你：";
            }
            content += commentSendDto.getContent();
            notice.setContent(content);
            notice.setVideoId(commentSendDto.getVideoId());
            this.saveNotice(notice);
        }

    }
}
